home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / PCL4C33.ARJ / CRC.C < prev    next >
Text File  |  1992-08-01  |  759b  |  38 lines

  1. #include <stdio.h>
  2. #define POLY 0x1021
  3.  
  4. unsigned short CRCtable[256];
  5.  
  6. /* initialize CRC table */
  7.  
  8. void InitCRC()
  9. {int i;
  10.  unsigned short CalcTable();
  11.  for(i=0;i<256;i++) CRCtable[i] = CalcTable(i,POLY,0);
  12. }
  13.  
  14. /* calculate CRC table entry */
  15.  
  16. unsigned short CalcTable(data,genpoly,accum)
  17. unsigned short data;
  18. unsigned short genpoly;
  19. unsigned short accum;
  20. {static int i;
  21.  data <<= 8;
  22.  for(i=8;i>0;i--)
  23.          {
  24.           if((data^accum) & 0x8000) accum = (accum << 1) ^ genpoly;
  25.           else accum <<= 1;
  26.           data <<= 1;
  27.          }
  28.  return(accum);
  29. }
  30.  
  31. /* compute updated CRC */
  32.  
  33. unsigned short UpdateCRC(crc,byte)
  34. unsigned short crc;
  35. unsigned char  byte;
  36. {
  37.  return( (crc << 8) ^ CRCtable[ (crc >> 8) ^ byte ] );
  38. }